package com.example.drouce.json_parse;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by drouce on 16/4/12.
 */
public class JsonParse {


    public List<Person> parseJSON (String jsonString) {
        List<Person> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(jsonString);
            String title = object.getString("title");
            JSONArray array = object.getJSONArray("person");

            Person person = null;

            for (int i = 0;i<array.length();i++) {
                JSONObject object1 = array.getJSONObject(i);
                String name = object1.getString("name");
                int age = object1.getInt("age");
                String sex = object1.getString("sex");
                String classes = object1.getString("class");
                person = new Person();
                person.names = name;
                person.age = age;
                person.setSex(sex);
                person.setClasses(classes);
                list.add(person);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
